package com.xiecong.controller;

import com.xiecong.aspect.annotation.LogOperation;
import com.xiecong.pojo.Clazz;
import com.xiecong.pojo.ClazzQueryParam;
import com.xiecong.pojo.PageResult;
import com.xiecong.pojo.Result;
import com.xiecong.service.ClazzServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/clazzs")
@Slf4j
@RestController
public class ClazzController {
    @Autowired
    private ClazzServer clazzServer;

    /**
     * 新增班级信息
     * @paramclazz
     * @return
     */
    @PostMapping
    @LogOperation
    public Result add(@RequestBody Clazz clazz){
        log.info("新增班级信息"+clazz);
        clazzServer.add(clazz);
        return Result.success();
    }


    /**
     * 查询所有班级信息
     * @return
     */
    @GetMapping("/list")
    public Result findAll(){
        log.info("查询所有班级信息");
        List<Clazz> clazzs = clazzServer.findAll();
        return Result.success(clazzs);
    }


    /**
     * 分页按条件查询班级列表
     */
    @GetMapping
    //public Result pageQuery(@RequestParam ClazzQueryParam clazzQueryParam){
      public Result pageQuery(ClazzQueryParam clazzQueryParam){
        log.info("分页按条件查询班级列表:{}",clazzQueryParam);
        PageResult<Clazz> pageQuery = clazzServer.pageQuery(clazzQueryParam);
        return Result.success(pageQuery);
    }


    /**
     * 修改班级信息-数据回显-根据id查询
     */
   @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
       Clazz clazz = clazzServer.findById(id);
       return Result.success(clazz);
   }


    /**
     * 修改班级信息
     */
    @PutMapping
    @LogOperation
    public Result updateById(@RequestBody Clazz clazz){
        clazzServer.updateById(clazz);
        return Result.success();
    }


    /**
     * 删除班级信息
     */
    @DeleteMapping("/{id}")
    @LogOperation
    public Result deleteById(@PathVariable Integer id){
        clazzServer.deleteById(id);
        return Result.success();
    }
}
